
#include "engine.h"

Parser::Parser() {
	
	script_land.load_library_xml();
	script_land.create_link(XLUA_parser_set_mapping,"set_map");
	script_land.create_link(XLUA_parser_get_mapping,"get_map");
	script_land.load_file( Util::create_filepath( Util::config["SYS"].stringStore["PATH"].store.sData , string("parser.lua") ).c_str() );
	script_land.call("init");
	script_land.remove_link("init");
	script_land.remove_link("set_map");
	script_land.remove_link("get_map");
	/* tag fn map initialization done */

	script_land.create_link(XLUA_library_new,"library_new");
	script_land.create_link(XLUA_library_get,"library_get");
	script_land.create_link(XLUA_library_set,"library_set");
	script_land.create_link(XLUA_canvas_new,"canvas_new");
	script_land.create_link(XLUA_canvas_get,"canvas_get");
	script_land.create_link(XLUA_canvas_set,"canvas_set");
	script_land.create_link(XLUA_config_new,"config_new");
	script_land.create_link(XLUA_config_get,"config_get");
	script_land.create_link(XLUA_config_set,"config_set");
	/* ready for parsing */
}



void Parser::read(const char *filepath) {
	TiXmlDocument doc(filepath);
	TiXmlNode * node;
	TiXmlElement *todoElement,*element; TiXmlPrinter xmlPrinter;
	string typeAttr;
	
	map<string,string,ltstr_stl>::iterator iter;
	node = doc.RootElement();
	if(!node) return;

	if(strcmp(node->Value(),"openanimator") != 0) return;

	todoElement = node->ToElement();

	for(element = todoElement->FirstChildElement(); element ; element = element->NextSiblingElement() )  {
		typeAttr = string(element->Attribute("type"));
		iter = mapping_tag_fn.find(typeAttr);
		if(iter == mapping_tag_fn.end()) { E("Parser::read : tag mapping not found. tag = " << typeAttr << endl) continue; }
		element->Accept(&xmlPrinter);
		script_land.call(iter->second,xmlPrinter.CStr());

	}
}
